home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / webmail / wisestampsig.js < prev    next >
Text File  |  2010-02-02  |  5KB  |  183 lines

  1. WiseStampLog("[wisestampsig.js] >>>>>");
  2. window.addEventListener("load", function (event) { wisestampsig.load(event) }, true);
  3. window.addEventListener("unload", function (event){ wisestampsig.unload(event) }, true);
  4.  
  5. var wisestampsig =
  6. {
  7.     PREFIX: "<!--WISESTAMP_SIG_START-->",
  8.     SUFFIX: "<!--WISESTAMP_SIG_END-->",
  9.     _prefs: null,
  10.     _icon: null,
  11.     _tempType: null,
  12.     load: function (event) {},
  13.     unload: function (event) {},
  14.     
  15.     get icon()
  16.     {
  17.         if (!this._icon)
  18.         {
  19.             this._icon = ToDataURI('chrome://wisestamp/skin/webmail_icon.png');
  20.         }
  21.         return this._icon;
  22.     },
  23.     
  24.     get enabled() 
  25.     {
  26.         return GetBoolValue("enabled");
  27.     },
  28.     
  29.     get showButton()
  30.     {
  31.         WiseStampLog("[wisestampsig.js::showButton get] >>>>>");
  32.         return this.enabled && !GetBoolValue("hidebuttons");
  33.     },
  34.     
  35.     get autoInsertOnCompose() 
  36.     {
  37.         return this.enabled && WiseStampPrefs.getBoolPref("autoinsert_compose",true,"autoinsert");
  38.     },
  39.     
  40.     get autoInsertOnThread() 
  41.     {
  42.         return this.enabled && WiseStampPrefs.getBoolPref("autoinsert_thread",true,"autoinsert");
  43.     },
  44.     
  45.     get isFoxytunesInstalled()
  46.     {
  47.         return HasExtension("{463F6CA5-EE3C-4be1-B7E6-7FEE11953374}");
  48.     },
  49.     
  50.     createSignature: function (aType)
  51.     {
  52.         WiseStampLog("[wisestampsig.js::createSignature] >>>>>");
  53.         var sig = GetSignature(aType);
  54.         if (sig) 
  55.             return this.PREFIX + sig + this.SUFFIX;
  56.         
  57.         return this.PREFIX + " " + this.SUFFIX;
  58.     },
  59.     
  60.     insertSignature: function (iframe, isAuto, xPath)
  61.     {
  62.         WiseStampLog("[wisestampsig.js::insertSignature] >>>>>");
  63.         if (!this.enabled) 
  64.             return;
  65.  
  66.         var body = iframe.contentDocument.body;
  67.     
  68.         var newMessage = true;
  69.         if (body.innerHTML.length > 100)
  70.             newMessage = false;
  71.         else        
  72.         {    // if > 100 - reply/forward. if < 100 - check how long the text only    
  73.             var text_body = strip_tags(body.innerHTML);
  74.             newMessage = text_body.length < 5; // sometimes a couple of chars still exist
  75.         }
  76.  
  77.         var atEnd = false;
  78.         if (isAuto)
  79.         {
  80.             if ((newMessage && !this.autoInsertOnCompose) ||
  81.                 (!newMessage && !this.autoInsertOnThread)) 
  82.                 return;
  83.         
  84.         } else if (!isAuto) 
  85.             atEnd = true;
  86.  
  87.         var sig = this.createSignature(this._tempType);
  88.         this._tempType = null;
  89.         var pattern = new RegExp(this.PREFIX.replace("!", "\!") + "(.|\n)*" + this.SUFFIX.replace("!", "\!"));
  90.         var inserted = false;
  91.  
  92.         if (body.innerHTML.match(pattern))
  93.         {
  94.             if (isAuto == false) // replace only if manually inserted
  95.             {
  96.                 WiseStampLog("[wisestampsig.js::insertSignature] replacing existing signature");
  97.                 body.innerHTML = body.innerHTML.replace(pattern, sig.replace('$','$$$$'));
  98.             }
  99.             
  100.         } else
  101.         {
  102.             if (xPath)
  103.             {
  104.                 var match = evaluateXPath(iframe.contentDocument, xPath);
  105.                 if (match && match.length > 0)
  106.                 {
  107.                     WiseStampLog("[wisestampsig.js::insertSignature] mode II");
  108.                     var div = document.createElement("p");
  109.                     div.setAttribute("class", "wisestamp_sig");
  110.                     div.innerHTML = sig;
  111.                     match[0].parentNode.insertBefore(div, match[0]);
  112.                     inserted = true;
  113.                 }
  114.             }
  115.             
  116.             if (!inserted && !atEnd)
  117.             {
  118.                 WiseStampLog("[wisestampsig.js::insertSignature] mode III");
  119.                 body.innerHTML = '<br/><br/>' + sig + body.innerHTML;
  120.                 inserted = true;
  121.                 
  122.             } else if (!inserted && atEnd)
  123.             {
  124.                 WiseStampLog("[wisestampsig.js::insertSignature] mode IV");
  125.                 body.innerHTML = body.innerHTML + '<br/><br/>' + sig;
  126.                 inserted = true;
  127.             }
  128.         }
  129.         
  130.         WiseStampLog("[wisestampsig.js::insertSignature] <<<< inserted = " + inserted);
  131.         
  132.         if (inserted)
  133.             WiseStampSigCreated();
  134.     },
  135.  
  136.     initOptionsIcon: function (aEle)
  137.     {
  138.         WiseStampLog("[wisestampsig.js::initOptionsIcon] >>>>>");
  139.             
  140.         aEle.addEventListener("click", this, false);
  141.         aEle.addEventListener("UpdateWisestampSignature", this, false);
  142.     },
  143.     
  144.     handleEvent: function (event)
  145.     {
  146.         if (event.type == "click")
  147.         {
  148.             this.optionsIconClicked(event);
  149.         } else if (event.type == "UpdateWisestampSignature")
  150.         {
  151.             WiseStampLog("[wisestampsig.js::handleEvent] event.type == UpdateWisestampSignature");
  152.             
  153.             if (this.refreshCallback)
  154.             {
  155.                 WiseStampLog("[wisestampsig.js::handleEvent] " + event.target.getAttribute("sigtype"));
  156.                 this._tempType = event.target.getAttribute("sigtype");
  157.                 this.refreshCallback();
  158.             }
  159.         }
  160.     },
  161.     
  162.     optionsIconClicked: function (event)
  163.     {
  164.         var evt = document.createEvent("MouseEvents");
  165.         evt.initMouseEvent("ShowWisestampMenu", true, false, window, event.detail, event.screenX, event.screenY, event.clientX, event.clientY, event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, event.button, event.relatedTarget);
  166.  
  167.         var canceled = !event.target.dispatchEvent(evt);
  168.     }
  169. }
  170.  
  171. function evaluateXPath(aNode, aExpr)
  172. {
  173.     var xpe = new XPathEvaluator();
  174.     var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ? aNode.documentElement : aNode.ownerDocument.documentElement);
  175.     var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
  176.     var foundxp = [];
  177.     var res;
  178.     var idx = 0;
  179.     while ((res = result.iterateNext()))
  180.         foundxp.push(res);
  181.     
  182.     return foundxp;
  183. }